package com.learn;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/*
 * 这个类用来对user表进行增删改查
 * */
public class UserMapper {
    public int insert(User user) {
        try (Connection conn = DBUtil.getConnection();) {
            String sql = "Insert into user(username,password,nickname,age)" + "values(?,?,?,?)";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, user.getName());
            ps.setString(2, user.getPassword());
            ps.setString(3, user.getNickname());
            ps.setInt(4, user.getAge());
            return ps.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /*
     * 根据用户名查询用户数据，如果用户不存在则返回null
     * @param username 用户名
     * @return User
     * */
    public User selectByName(String username) {
        try (Connection conn = DBUtil.getConnection();) {
            String sql = "select id, username,password, nickname,age from user where username = ? ";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, username);
            ResultSet rs = ps.executeQuery();
            if(rs.next()){
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setName(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setNickname(rs.getString("nickname"));
                user.setAge(rs.getInt("age"));
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    /*
    * 根据用户名修改用户数据
    * @param username 用户名
    * @return
    * */
    public int update(User user) {
        try (Connection conn = DBUtil.getConnection();) {
            String sql = "update user set password = ?, nickname = ?, age = ? where username = ?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, user.getPassword());
            ps.setString(2, user.getNickname());
            ps.setInt(3, user.getAge());
            ps.setString(4, user.getName());
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /*
    * 查询所有用户
    *
    * @return
    * */
    public List<User> selectAll() {
        List<User> users = new ArrayList<>();
        try (Connection connection = DBUtil.getConnection();){
            String sql = "select * from user";
            PreparedStatement ps = connection.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while(rs.next()){
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setName(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setNickname(rs.getString("nickname"));
                user.setAge(rs.getInt("age"));
                users.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
        return users;
    }
}
